package org.example.managementsystem.controller;

import org.example.managementsystem.model.DeviceScrap;
import org.example.managementsystem.model.DeviceScrapReview;
import org.example.managementsystem.service.DeviceScrapService;
import org.example.managementsystem.service.DeviceScrapReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备报废与审核表现层
 */
@RestController
@RequestMapping("/api/device/scraps")
public class DeviceScrapController {
    private final DeviceScrapService deviceScrapService;
    private final DeviceScrapReviewService deviceScrapReviewService;

    @Autowired
    public DeviceScrapController(DeviceScrapService deviceScrapService, DeviceScrapReviewService deviceScrapReviewService) {
        this.deviceScrapService = deviceScrapService;
        this.deviceScrapReviewService = deviceScrapReviewService;
    }

    /**
     * 提交设备报废申请
     */
    @PostMapping("/submit")
    public ResponseEntity<String> submitScrap(@RequestBody DeviceScrap scrap) {
        // 保证数据安全，设置申请状态为待审核
        scrap.setIsApproved(false);
        scrap.setStatus(false);
        deviceScrapService.submitScrap(scrap);
        return ResponseEntity.ok("设备报废申请提交成功");
    }

    /**
     * 审核设备报废申请并记录审核信息
     */
    @PutMapping("/review")
    public ResponseEntity<String> reviewScrap(@RequestBody DeviceScrapReview review) {
        deviceScrapReviewService.reviewScrap(review);
        return ResponseEntity.ok("设备报废申请审核成功");
    }

    /**
     * 更新报废申请信息
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateScrap(@RequestBody DeviceScrap scrap) {
        deviceScrapService.updateScrap(scrap);
        return ResponseEntity.ok("设备报废申请更新成功");
    }

    /**
     * 获取所有报废申请
     */
    @GetMapping("/getScrap")
    public ResponseEntity<List<DeviceScrap>> getAllScraps() {
        List<DeviceScrap> scraps = deviceScrapService.getAllScraps();
        return ResponseEntity.ok(scraps);
    }

    /**
     * 获取所有未审核的报废申请
     */
    @GetMapping("/getUnreviewedScrap")
    public ResponseEntity<List<DeviceScrap>> getAllUnreviewedScraps() {
        List<DeviceScrap> scraps = deviceScrapService.getAllUnreviewedScraps();
        return ResponseEntity.ok(scraps);
    }

    /**
     * 根据ID获取报废申请
     */
    @GetMapping("/getScrapById/{scrapId}")
    public ResponseEntity<DeviceScrap> getScrapById(@PathVariable int scrapId) {
        DeviceScrap scrap = deviceScrapService.getScrapById(scrapId);
        return ResponseEntity.ok(scrap);
    }

    /**
     * 根据ID获取审核记录
     */
    @GetMapping("/getReviewById/{scrapId}")
    public ResponseEntity<DeviceScrapReview> getReviewById(@PathVariable int scrapId) {
        DeviceScrapReview review = deviceScrapReviewService.getReviewById(scrapId);
        if (review == null) {
            return ResponseEntity.notFound().build(); // 返回404状态
        }
        return ResponseEntity.ok(review);
    }

    /**
     * 根据ID删除报废申请
     */
    @DeleteMapping("/delete/{scrapId}")
    public ResponseEntity<String> deleteScrap(@PathVariable int scrapId) {
        deviceScrapService.deleteScrap(scrapId);
        return ResponseEntity.ok("设备报废申请删除成功");
    }

    /**
     * 根据报修ID获取报废申请记录
     */
    @GetMapping("/getScrapByRepairId/{repairId}")
    public ResponseEntity<List<DeviceScrap>> getScrapByRepairId(@PathVariable int repairId) {
        List<DeviceScrap> scraps = deviceScrapService.getScrapByRepairId(repairId);
        return ResponseEntity.ok(scraps);
    }
}